package com.kaili.basic.service.impl;

import com.kaili.basic.mapper.DeviceMapper;
import com.kaili.basic.mapper.MdmDeviceCommandMapper;
import com.kaili.basic.pojo.dto.MdmDeviceCommandAddDto;
import com.kaili.basic.pojo.dto.MdmDeviceCommandDto;
import com.kaili.basic.pojo.entity.MdmDeviceCommand;
import com.kaili.basic.pojo.vo.MdmDeviceCommandVo;
import com.kaili.basic.pojo.vo.MdmDeviceOnlineVo;
import com.kaili.basic.service.MdmDeviceCommandService;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Desc: 设备命令serviceImpl
 * @author: shenliqiang
 * @Time: 2019/7/26 9:07
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@Service
@Transactional
public class MdmDeviceCommandServiceImpl implements MdmDeviceCommandService {
    @Autowired
    private MdmDeviceCommandMapper mdmDeviceCommandMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public RestStatus addMdmDeviceCommand(MdmDeviceCommandAddDto dto) {
        MdmDeviceCommand mdmDeviceCommand = new MdmDeviceCommand();
        BeanUtils.copyProperties(dto, mdmDeviceCommand);
        mdmDeviceCommand.setCreateBy(LoginDetailUtils.getCurrent().getId());
        mdmDeviceCommandMapper.addMdmDeviceCommand(mdmDeviceCommand);
        return CommonStatus.ADD_OK;
    }

    @Override
    public RestStatus updateMdmDeviceCommandStatus(List<MdmDeviceCommandDto> dtos) {
        for (MdmDeviceCommandDto dto:dtos) {
            //若设备已被删除，则本记录删除标识也改为0（以删除），设备表改回正常时调用update接口修改状态
            if(dto.getDeviceId()==null){
                dto.setDeviceId(deviceMapper.getIsActionByImei(dto.getImei()).getId());
            }
            MdmDeviceCommand mdmDeviceCommand = new MdmDeviceCommand();
            BeanUtils.copyProperties(dto, mdmDeviceCommand);
            mdmDeviceCommand.setModifyBy(LoginDetailUtils.getCurrent().getId());
            mdmDeviceCommandMapper.updateMdmDeviceCommandStatus(mdmDeviceCommand);
        }
        return CommonStatus.DEVICECOMMAND_CHANGE_SUCCESS;
    }

    @Override
    public RestStatus updateMdmDeviceCommandStatusByDevice(MdmDeviceCommandDto dto) {
        //若设备已被删除，则本记录删除标识也改为0（以删除），设备表改回正常时调用update接口修改状态
        if(dto.getDeviceId()==null){
            dto.setDeviceId(deviceMapper.getIsActionByImei(dto.getImei()).getId());
        }
        MdmDeviceCommand mdmDeviceCommand = new MdmDeviceCommand();
        BeanUtils.copyProperties(dto, mdmDeviceCommand);
        mdmDeviceCommand.setModifyBy(LoginDetailUtils.getCurrent().getId());
        mdmDeviceCommandMapper.updateMdmDeviceCommandStatus(mdmDeviceCommand);
        return CommonStatus.DEVICECOMMAND_CHANGE_SUCCESS;
    }

    @Override
    public MdmDeviceCommandVo queryMdmDeviceCommand(String imei) {
        return mdmDeviceCommandMapper.queryMdmDeviceCommand(imei);
    }
}
